﻿using App.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            headers.Add("Referer", "http://finance.sina.com.cn");
            textBox1.Text = "sh601595";
            this.Opacity = 0.3;
        }

        public decimal oldPrice = 0;
        public Dictionary<string, string> headers = new Dictionary<string, string>();

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
            timer1.Interval = 3000;
            timer1.Tick += Timer1_Tick;
            timer1.Start();

            timer2.Interval = 3000;
            timer2.Tick += Timer2_Tick; ;
            timer2.Start();
        }

        private void Timer2_Tick(object sender, EventArgs e)
        {
            try
            {
            var code = textBox1.Text.Trim().ToLower();

            var result = HttpHelper.HttpGet($"http://hq.sinajs.cn/list={code}", null, headers);

            var array = result.Split(',');
            var nowPrice = decimal.Parse(array[3].ToString());
            if (oldPrice < nowPrice)
            {
                label1.ForeColor = Color.Red;
            }
            else if (oldPrice == nowPrice)
            {
            }
            else
            {
                label1.ForeColor = Color.Green;
            }

            label1.Text = nowPrice.ToString();
            oldPrice = nowPrice;

            Price(result);

            }
            catch (Exception)
            {

            }

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            try
            {

            var code = textBox1.Text.Trim().ToLower();
            webBrowser1.Navigate($"http://image.sinajs.cn/newchart/min/n/{code}.gif");
            }
            catch (Exception)
            {
            }
        }

        //预测后面几秒价格的价格趋势
        private void Price(string result)
        {
            var array = result.Split(',');
            var buy1 = decimal.Parse(array[10]) * decimal.Parse(array[11]);
            var buy2 = decimal.Parse(array[12]) * decimal.Parse(array[13]);
            var buy3 = decimal.Parse(array[14]) * decimal.Parse(array[15]);
            var buy4 = decimal.Parse(array[16]) * decimal.Parse(array[17]);
            var buy5 = decimal.Parse(array[18]) * decimal.Parse(array[19]);
            var buy = buy1 + buy2 + buy3 + buy4 + buy5;

            var sell1 = decimal.Parse(array[20]) * decimal.Parse(array[21]);
            var sell2 = decimal.Parse(array[22]) * decimal.Parse(array[23]);
            var sell3 = decimal.Parse(array[24]) * decimal.Parse(array[25]);
            var sell4 = decimal.Parse(array[26]) * decimal.Parse(array[27]);
            var sell5 = decimal.Parse(array[28]) * decimal.Parse(array[29]);
            var sell = sell1 + sell2 + sell3 + sell4 + sell5;

            var aa = buy - sell;
            if (aa > 0)
            {
                label2.ForeColor = Color.Red;
                label2.Text = $"实时预测:[趋势上涨]买-卖={aa/100m}(万元)";
                //上涨趋势
            }
            if (aa < 0)
            {
                label2.ForeColor = Color.Green;
                label2.Text = $"实时预测:[趋势下跌]买-卖={aa/100m}(万元)";
                //下跌趋势
            }


        }


    }
}
